#include "bullet3.h"

Bullet3::Bullet3()
{
    //加载子弹资源
    m_Bullet.load(ENEMY1_PATH);
    m_BulletDown[0].load(ENEMY1_DOWN1);
    m_BulletDown[1].load(ENEMY1_DOWN2);
    m_BulletDown[2].load(ENEMY1_DOWN3);
    m_BulletDown[3].load(ENEMY1_DOWN4);
    m_BulletDown[4].load(ENEMY1_DOWN5);
    m_BulletDown[5].load(ENEMY1_DOWN6);
    m_BulletDown[6].load(ENEMY1_DOWN7);
    p_Bullet = &m_Bullet;


    //子弹坐标初始化
    m_X = 0;
    m_Y = 0;

    //子弹移动速度
    m_Speed = ENEMY1_SPEED;

    //子弹闲置
    m_Free = true;

    //子弹的矩形边框
    m_Rect.setWidth(m_Bullet.width());
    m_Rect.setHeight(m_Bullet.height());
    m_Rect.moveTo(m_X, m_Y);

    m_Down = -1;
}

/* 设置子弹坐标 */
void Bullet3::setPosition(int x, int y)
{
    m_X = x - m_Bullet.width()/2;
    m_Y = y - m_Bullet.height()/2;
    m_Rect.moveTo(m_X, m_Y);
}

/* 更新子弹坐标 */
void Bullet3::updatePosition()
{
    //如果子弹闲置不更新
    if(m_Free)
    {
        return;
    }
    //爆炸处理
    if(m_Down >= 0)
    {
        if(m_Down % ENEMY1_DOWN_INTERVAL == 0)
        {
            if(m_Down >= ENEMY1_DOWN_NUM*ENEMY1_DOWN_INTERVAL)
            {
                //初始化
                p_Bullet = &m_Bullet;
                m_Down = -1;
                m_Free = true;
                setPosition(0, 0);
                m_Rect.moveTo(m_X, m_Y);
                return;
            }
            else
            {
                p_Bullet = &m_BulletDown[m_Down/ENEMY1_DOWN_INTERVAL];
            }
        }
        m_Down++;
        return;
    }

    //子弹Y坐标加
    m_Y += m_Speed;
    m_Rect.moveTo(m_X, m_Y);

    //如果子弹射出屏幕更新为闲置
    if(m_Y > GAME_HEIGHT)
    {
        m_Free = true;
    }
}

